﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace TorchLightSettings
{
    public partial class Form1 : Form
    {
        private KeyBindingButton _activeButton;
        private bool _interceptTab = false;
        protected TLSettings _settings = new TLSettings();

        public Form1()
        {
            InitializeComponent();
        }

        private void keyBindingButton_Click(object sender, EventArgs e)
        {
            if (_activeButton != null)
                _activeButton.BackColor = Color.White;
            
            _activeButton = (KeyBindingButton)sender;

            _activeButton.BackColor = Color.Yellow;
            _interceptTab = true;
        }

        private void keyBindingButton_KeyDown(object sender, KeyEventArgs e)
        {
            if (_activeButton == (KeyBindingButton)sender)
            {
                _activeButton.KeyCode = Convert.ToInt32(e.KeyCode);
                
                e.Handled = true;                                

                _activeButton.BackColor = Color.White;
                _activeButton = null;                
            }

            listBox1.Items.Add(_settings.KeyBindings.ActionBar1.ToString());
            _interceptTab = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            _settings.Load("settings.txt");

            AssignButtonBindings();
        }

        private void AssignButtonBindings()
        {
            btnActionBar1.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar1", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar2.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar2", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar3.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar3", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar4.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar4", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar5.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar5", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar6.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar6", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar7.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar7", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar8.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar8", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar9.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar9", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
            btnActionBar0.DataBindings.Add(new Binding("KeyCode", _settings.KeyBindings, "ActionBar0", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));
        }

        private void keyBindingButton_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void keyBindingButton_KeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        protected override bool ProcessTabKey(bool forward)
        {
            if (_interceptTab)
                return false;
            else
                return base.ProcessTabKey(forward);
        }
    }
}
